package com.yphone.ui.login.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.zzkj.yunphone.R;
import com.yphone.tools.Constants;
import com.yphone.ui.dialog.CommSmallLoadingDialog;
import com.yphone.ui.event.MyEvent;
import com.yphone.ui.login.contract.RegisterActivityContract;
import com.yphone.ui.login.presenter.RegisterActivityPresenter;
import com.yphone.ui.login.view.PwdView;
import com.yphone.ui.login.view.TelView;
import com.yphone.ui.login.view.VerifiView;
import com.yphone.utils.NetworkUtils;
import com.yphone.utils.RegexUtils;
import com.yphone.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

public class RegisterActivity extends AppCompatActivity implements RegisterActivityContract.IView, OnClickListener {
    private CheckBox mCkbAgreement;
    private PwdView mEdtPassword;
    private TelView mEdtPhoneNum;
    private RegisterActivityContract.IPresenter mIPresenter;
    private ImageView mImgBack;
    private ImageView mImgPhoneNumClean;
    private EditText mInviteCode;
    private TextView mTxtAgreement;
    private TextView mTxtHasAccountLogin;
    private TextView mTxtPhoneRegisterFormatRemind;
    private TextView mTxtRegisterAndLogin;
    private VerifiView mVerifiView;

    public static void toRegisterActivity(Context context) {
        context.startActivity(new Intent(context, RegisterActivity.class));
    }

    /* access modifiers changed from: protected */
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView((int) R.layout.activity_register);
        EventBus.getDefault().register(this);
        initDataBeforView();
        initView();
        initData();
        initListener();
//        StatisticsManager.getInstance().BuriedPoint(101);
    }

    public void initDataBeforView() {
    }

    public void initView() {
        this.mImgBack = (ImageView) findViewById(R.id.activity_register_img_back);
        this.mTxtPhoneRegisterFormatRemind = (TextView) findViewById(R.id.activity_register_txt_phone_register_remind);
        this.mTxtPhoneRegisterFormatRemind.setVisibility(View.GONE);
        this.mEdtPhoneNum = (TelView) findViewById(R.id.activity_register_edt_phone_number);
        this.mImgPhoneNumClean = (ImageView) findViewById(R.id.activity_register_img_phone_number_clean);
        this.mEdtPassword = (PwdView) findViewById(R.id.activity_register_edt_password);
        this.mVerifiView = (VerifiView) findViewById(R.id.activity_register_verifi_view);
        this.mInviteCode = (EditText) findViewById(R.id.inviteCode);
        this.mVerifiView.setInfo(this, 1);
        this.mCkbAgreement = (CheckBox) findViewById(R.id.activity_register_ckb_agreement);
        this.mTxtAgreement = (TextView) findViewById(R.id.activity_register_txt_agreement);
        this.mTxtRegisterAndLogin = (TextView) findViewById(R.id.activity_register_txt_register_and_login);
        this.mTxtHasAccountLogin = (TextView) findViewById(R.id.activity_register_txt_has_account_login);
        this.mTxtAgreement.getPaint().setFlags(8);
    }

    public void initData() {
        this.mIPresenter = new RegisterActivityPresenter(this);
    }

    public void initListener() {
        this.mImgBack.setOnClickListener(this);
        this.mImgPhoneNumClean.setOnClickListener(this);
        this.mTxtRegisterAndLogin.setOnClickListener(this);
        this.mTxtHasAccountLogin.setOnClickListener(this);
        this.mCkbAgreement.setOnClickListener(this);
        this.mTxtAgreement.setOnClickListener(this);
    }

    public void onClick(View view) {
        if (view.getId() == this.mImgBack.getId()) {
            finish();
        } else if (view.getId() == this.mImgPhoneNumClean.getId()) {
            this.mEdtPhoneNum.setText("");
        } else if (view.getId() == this.mTxtRegisterAndLogin.getId()) {
            submit();
        } else if (view.getId() == this.mCkbAgreement.getId()) {
        } else {
            if (view.getId() == this.mTxtAgreement.getId()) {
//                String webUrl = PresetManger.getInstance().getAgreeMentPage();
//                if (!TextUtils.isEmpty(webUrl)) {
//                    WXPayEntryActivity.toWXPayEntryActivity(this, webUrl, "注册协议", 0);
//                }
            } else if (view.getId() == this.mTxtHasAccountLogin.getId()) {
                LoginActivity.toLoginActivity(this);
            }
        }
    }

    public void submit() {
        String phone_number = this.mEdtPhoneNum.getText().toString();
        String inviteCode = this.mInviteCode.getText().toString();
        if (TextUtils.isEmpty(phone_number)) {
            ToastUtils.showLong((CharSequence) getResources().getString(R.string.login_phone_empty));
        } else if (phone_number.length() != 11) {
            ToastUtils.showLong((CharSequence) getString(R.string.phone_format));
        } else if (TextUtils.isEmpty(inviteCode)) {
            ToastUtils.showLong((CharSequence) getString(R.string.phone_invoke_not_null));
        } else {
            String password = this.mEdtPassword.getPassword();
            if (TextUtils.isEmpty(password)) {
                ToastUtils.showLong((CharSequence) getResources().getString(R.string.login_pwd_empty));
            } else if (password.length() < 6) {
                ToastUtils.showLong((CharSequence) getResources().getString(R.string.login_pwd_six));
            } else if (!RegexUtils.isMatch(Constants.PWD_MATCHING, password)) {
                ToastUtils.showLong((CharSequence) getResources().getString(R.string.mmgssryw));
            } else {
                String verificationCode = this.mVerifiView.getVerifiCode();
                if (TextUtils.isEmpty(verificationCode)) {
                    ToastUtils.showLong((CharSequence) "验证码不能为空!");
                } else if (verificationCode.length() < 4) {
                    ToastUtils.showLong((CharSequence) getResources().getString(R.string.login_verifi_code_four));
                } else {
                    if (!this.mCkbAgreement.isChecked()) {
                        ToastUtils.showLong((CharSequence) "请勾选协议!");
                    } else if (!NetworkUtils.isConnected()) {
                        ToastUtils.showLong((CharSequence) getString(R.string.no_net));
                    } else {
                        CommSmallLoadingDialog.showDialog(this, getString(R.string.request_ing));
                        this.mIPresenter.registerRequest(phone_number, password, verificationCode, inviteCode);
                    }
                }
            }
        }
    }

    public String getPhoneNumber() {
        return this.mEdtPhoneNum.getText().toString();
    }

    public Context getContext() {
        return this;
    }

    /* access modifiers changed from: protected */
    public void onDestroy() {
        super.onDestroy();
        this.mIPresenter.destory();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void onEventMainThread(MyEvent.LoginSuccessEvent event) {
        finish();
    }

    @Subscribe
    public void onEventMainThread(MyEvent.GetUserInfoSuccessEvent event) {
        finish();
    }

}
